/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bsc.war.mail;

import bsc.ejb.metadata.SMTP;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.Transport;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
import java.security.Security;
import javax.mail.Address;

public class MailSender {

    private Properties props = null;
    private Session session;
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static MailSender _instance = null;
    private String host;
    private String from;
    private String pass;


    private MailSender(String from, String host, final String user, final String password, boolean secure, String port) {
        this.from = from;
        this.host = host;
        this.from = user;
        this.pass = password;
        props = new Properties();
          //props.setProperty( "mail.pop3.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.starttls.enable", "true"); // added this line
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "false");
        props.put("mail.smtp.port", port);
        if (secure) {
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            props.put("mail.smtp.socketFactory.port", port);
            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");
        }
        //this.user = user;
        //this.password = password;
        session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, password);
                    }
                });

        session.setDebug(false);
    }

    public static MailSender getInstance(SMTP smtp) {
        if (_instance == null) {
            _instance = new MailSender(smtp.getName(), smtp.getSmtpServer(), smtp.getMail(), smtp.getPassword(), MailUtils.AUTH, MailUtils.PORT);
        }
        return _instance;
    }

    public void send(String from, String subject, String recipient, String message, String contentType) throws Exception {


        Message msg = new MimeMessage(session);
        /*
        Address addressFrom = new InternetAddress(from, this.from);
        msg.setFrom(addressFrom);
        */
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, contentType);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.send(msg);
        // transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}

